package o;

import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import javax.annotation.Nullable;
import okhttp3.internal.http2.StreamResetException;

public final class fwh {
    static final /* synthetic */ boolean ᐝ = (!fwh.class.desiredAssertionStatus());
    final e ʻ = new e(this);
    private final Deque<fui> ʼ = new ArrayDeque();
    fwf ʽ = null;
    long ˊ;
    long ˋ = 0;
    private boolean ˋॱ;
    final b ˎ;
    final fwk ˏ;
    private d ˏॱ;
    private final d ͺ;
    final int ॱ;
    final e ॱॱ = new e(this);

    final class b implements fxx {
        static final /* synthetic */ boolean ˏ = (!fwh.class.desiredAssertionStatus());
        boolean ˊ;
        boolean ˋ;
        private final fxi ˎ = new fxi();
        final /* synthetic */ fwh ॱ;

        b(fwh o_fwh) {
            this.ॱ = o_fwh;
        }

        public void ˎ(fxi o_fxi, long j) throws IOException {
            if (ˏ || !Thread.holdsLock(this.ॱ)) {
                this.ˎ.ˎ(o_fxi, j);
                while (this.ˎ.ॱ() >= 16384) {
                    ॱ(false);
                }
                return;
            }
            throw new AssertionError();
        }

        private void ॱ(boolean z) throws IOException {
            synchronized (this.ॱ) {
                this.ॱ.ʻ.ar_();
                while (this.ॱ.ˊ <= 0 && !this.ˊ && !this.ˋ && this.ॱ.ʽ == null) {
                    try {
                        this.ॱ.ͺ();
                    } catch (Throwable th) {
                        this.ॱ.ʻ.au_();
                    }
                }
                this.ॱ.ʻ.au_();
                this.ॱ.ˏॱ();
                long min = Math.min(this.ॱ.ˊ, this.ˎ.ॱ());
                fwh o_fwh = this.ॱ;
                o_fwh.ˊ -= min;
            }
            this.ॱ.ʻ.ar_();
            try {
                fwk o_fwk = this.ॱ.ˏ;
                int i = this.ॱ.ॱ;
                boolean z2 = z && min == this.ˎ.ॱ();
                o_fwk.ˊ(i, z2, this.ˎ, min);
            } finally {
                this.ॱ.ʻ.au_();
            }
        }

        public void flush() throws IOException {
            if (ˏ || !Thread.holdsLock(this.ॱ)) {
                synchronized (this.ॱ) {
                    this.ॱ.ˏॱ();
                }
                while (this.ˎ.ॱ() > 0) {
                    ॱ(false);
                    this.ॱ.ˏ.ˋ();
                }
                return;
            }
            throw new AssertionError();
        }

        public fxv ˊ() {
            return this.ॱ.ʻ;
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void close() throws java.io.IOException {
            /*
            r6 = this;
            r4 = 0;
            r2 = 1;
            r0 = ˏ;
            if (r0 != 0) goto L_0x0015;
        L_0x0007:
            r0 = r6.ॱ;
            r0 = java.lang.Thread.holdsLock(r0);
            if (r0 == 0) goto L_0x0015;
        L_0x000f:
            r0 = new java.lang.AssertionError;
            r0.<init>();
            throw r0;
        L_0x0015:
            r1 = r6.ॱ;
            monitor-enter(r1);
            r0 = r6.ˋ;	 Catch:{ all -> 0x003f }
            if (r0 == 0) goto L_0x001e;
        L_0x001c:
            monitor-exit(r1);	 Catch:{ all -> 0x003f }
        L_0x001d:
            return;
        L_0x001e:
            monitor-exit(r1);	 Catch:{ all -> 0x003f }
            r0 = r6.ॱ;
            r0 = r0.ˎ;
            r0 = r0.ˊ;
            if (r0 != 0) goto L_0x004e;
        L_0x0027:
            r0 = r6.ˎ;
            r0 = r0.ॱ();
            r0 = (r0 > r4 ? 1 : (r0 == r4 ? 0 : -1));
            if (r0 <= 0) goto L_0x0042;
        L_0x0031:
            r0 = r6.ˎ;
            r0 = r0.ॱ();
            r0 = (r0 > r4 ? 1 : (r0 == r4 ? 0 : -1));
            if (r0 <= 0) goto L_0x004e;
        L_0x003b:
            r6.ॱ(r2);
            goto L_0x0031;
        L_0x003f:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x003f }
            throw r0;
        L_0x0042:
            r0 = r6.ॱ;
            r0 = r0.ˏ;
            r1 = r6.ॱ;
            r1 = r1.ॱ;
            r3 = 0;
            r0.ˊ(r1, r2, r3, r4);
        L_0x004e:
            r1 = r6.ॱ;
            monitor-enter(r1);
            r0 = 1;
            r6.ˋ = r0;	 Catch:{ all -> 0x0062 }
            monitor-exit(r1);	 Catch:{ all -> 0x0062 }
            r0 = r6.ॱ;
            r0 = r0.ˏ;
            r0.ˋ();
            r0 = r6.ॱ;
            r0.ॱॱ();
            goto L_0x001d;
        L_0x0062:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x0062 }
            throw r0;
            */
            throw new UnsupportedOperationException("Method not decompiled: o.fwh.b.close():void");
        }
    }

    final class d implements fxw {
        static final /* synthetic */ boolean ˎ = (!fwh.class.desiredAssertionStatus());
        private final long ʼ;
        private final fxi ˊ = new fxi();
        boolean ˋ;
        boolean ˏ;
        final /* synthetic */ fwh ॱ;
        private final fxi ᐝ = new fxi();

        d(fwh o_fwh, long j) {
            this.ॱ = o_fwh;
            this.ʼ = j;
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j);
            }
            fwf o_fwf;
            while (true) {
                synchronized (this.ॱ) {
                    long j2;
                    this.ॱ.ॱॱ.ar_();
                    try {
                        if (this.ॱ.ʽ != null) {
                            o_fwf = this.ॱ.ʽ;
                        } else {
                            o_fwf = null;
                        }
                        if (this.ˏ) {
                            throw new IOException("stream closed");
                        }
                        d ˏ;
                        fui o_fui;
                        if (!this.ॱ.ʼ.isEmpty() && this.ॱ.ˏॱ != null) {
                            ˏ = this.ॱ.ˏॱ;
                            o_fui = (fui) this.ॱ.ʼ.removeFirst();
                            j2 = -1;
                        } else if (this.ᐝ.ॱ() > 0) {
                            j2 = this.ᐝ.ˊ(o_fxi, Math.min(j, this.ᐝ.ॱ()));
                            fwh o_fwh = this.ॱ;
                            o_fwh.ˋ += j2;
                            if (o_fwf != null || this.ॱ.ˋ < ((long) (this.ॱ.ˏ.ॱˊ.ॱ() / 2))) {
                                ˏ = null;
                                o_fui = null;
                            } else {
                                this.ॱ.ˏ.ॱ(this.ॱ.ॱ, this.ॱ.ˋ);
                                this.ॱ.ˋ = 0;
                                ˏ = null;
                                o_fui = null;
                            }
                        } else if (this.ˋ || o_fwf != null) {
                            j2 = -1;
                            ˏ = null;
                            o_fui = null;
                        } else {
                            this.ॱ.ͺ();
                            this.ॱ.ॱॱ.au_();
                        }
                        this.ॱ.ॱॱ.au_();
                        if (o_fui != null && ˏ != null) {
                            ˏ.ˊ(o_fui);
                        }
                    } catch (Throwable th) {
                        this.ॱ.ॱॱ.au_();
                    }
                }
            }
            if (j2 != -1) {
                ˊ(j2);
                return j2;
            } else if (o_fwf == null) {
                return -1;
            } else {
                throw new StreamResetException(o_fwf);
            }
        }

        private void ˊ(long j) {
            if (ˎ || !Thread.holdsLock(this.ॱ)) {
                this.ॱ.ˏ.ˋ(j);
                return;
            }
            throw new AssertionError();
        }

        void ˎ(fxg o_fxg, long j) throws IOException {
            if (ˎ || !Thread.holdsLock(this.ॱ)) {
                while (j > 0) {
                    boolean z;
                    Object obj;
                    synchronized (this.ॱ) {
                        z = this.ˋ;
                        obj = this.ᐝ.ॱ() + j > this.ʼ ? 1 : null;
                    }
                    if (obj != null) {
                        o_fxg.ʼ(j);
                        this.ॱ.ॱ(fwf.FLOW_CONTROL_ERROR);
                        return;
                    } else if (z) {
                        o_fxg.ʼ(j);
                        return;
                    } else {
                        long ˊ = o_fxg.ˊ(this.ˊ, j);
                        if (ˊ == -1) {
                            throw new EOFException();
                        }
                        j -= ˊ;
                        synchronized (this.ॱ) {
                            if (this.ᐝ.ॱ() == 0) {
                                obj = 1;
                            } else {
                                obj = null;
                            }
                            this.ᐝ.ˋ(this.ˊ);
                            if (obj != null) {
                                this.ॱ.notifyAll();
                            }
                        }
                    }
                }
                return;
            }
            throw new AssertionError();
        }

        public fxv ˊ() {
            return this.ॱ.ॱॱ;
        }

        public void close() throws IOException {
            d dVar;
            List list = null;
            synchronized (this.ॱ) {
                this.ˏ = true;
                long ॱ = this.ᐝ.ॱ();
                this.ᐝ.ʿ();
                if (this.ॱ.ʼ.isEmpty() || this.ॱ.ˏॱ == null) {
                    dVar = null;
                } else {
                    ArrayList arrayList = new ArrayList(this.ॱ.ʼ);
                    this.ॱ.ʼ.clear();
                    list = arrayList;
                    dVar = this.ॱ.ˏॱ;
                }
                this.ॱ.notifyAll();
            }
            if (ॱ > 0) {
                ˊ(ॱ);
            }
            this.ॱ.ॱॱ();
            if (dVar != null) {
                for (fui ˊ : r0) {
                    dVar.ˊ(ˊ);
                }
            }
        }
    }

    class e extends fxd {
        final /* synthetic */ fwh ˎ;

        e(fwh o_fwh) {
            this.ˎ = o_fwh;
        }

        protected void ॱ() {
            this.ˎ.ॱ(fwf.CANCEL);
        }

        protected IOException ˋ(IOException iOException) {
            IOException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        public void au_() throws IOException {
            if (ˋ()) {
                throw ˋ(null);
            }
        }
    }

    fwh(int i, fwk o_fwk, boolean z, boolean z2, @Nullable fui o_fui) {
        if (o_fwk == null) {
            throw new NullPointerException("connection == null");
        }
        this.ॱ = i;
        this.ˏ = o_fwk;
        this.ˊ = (long) o_fwk.ͺ.ॱ();
        this.ͺ = new d(this, (long) o_fwk.ॱˊ.ॱ());
        this.ˎ = new b(this);
        this.ͺ.ˋ = z2;
        this.ˎ.ˊ = z;
        if (o_fui != null) {
            this.ʼ.add(o_fui);
        }
        if (ˊ() && o_fui != null) {
            throw new IllegalStateException("locally-initiated streams shouldn't have headers yet");
        } else if (!ˊ() && o_fui == null) {
            throw new IllegalStateException("remotely-initiated streams should have headers");
        }
    }

    public int ˏ() {
        return this.ॱ;
    }

    public synchronized boolean ॱ() {
        boolean z = false;
        synchronized (this) {
            if (this.ʽ == null) {
                if (!((this.ͺ.ˋ || this.ͺ.ˏ) && ((this.ˎ.ˊ || this.ˎ.ˋ) && this.ˋॱ))) {
                    z = true;
                }
            }
        }
        return z;
    }

    public boolean ˊ() {
        boolean z;
        if ((this.ॱ & 1) == 1) {
            z = true;
        } else {
            z = false;
        }
        return this.ˏ.ˋ == z;
    }

    public synchronized fui ˋ() throws IOException {
        this.ॱॱ.ar_();
        while (this.ʼ.isEmpty() && this.ʽ == null) {
            try {
                ͺ();
            } catch (Throwable th) {
                this.ॱॱ.au_();
            }
        }
        this.ॱॱ.au_();
        if (this.ʼ.isEmpty()) {
            throw new StreamResetException(this.ʽ);
        }
        return (fui) this.ʼ.removeFirst();
    }

    public fxv ˎ() {
        return this.ॱॱ;
    }

    public fxv ᐝ() {
        return this.ʻ;
    }

    public fxw ʼ() {
        return this.ͺ;
    }

    public fxx ʻ() {
        synchronized (this) {
            if (this.ˋॱ || ˊ()) {
            } else {
                throw new IllegalStateException("reply before requesting the sink");
            }
        }
        return this.ˎ;
    }

    public void ˎ(fwf o_fwf) throws IOException {
        if (ˏ(o_fwf)) {
            this.ˏ.ˎ(this.ॱ, o_fwf);
        }
    }

    public void ॱ(fwf o_fwf) {
        if (ˏ(o_fwf)) {
            this.ˏ.ˊ(this.ॱ, o_fwf);
        }
    }

    private boolean ˏ(fwf o_fwf) {
        if (ᐝ || !Thread.holdsLock(this)) {
            synchronized (this) {
                if (this.ʽ != null) {
                    return false;
                } else if (this.ͺ.ˋ && this.ˎ.ˊ) {
                    return false;
                } else {
                    this.ʽ = o_fwf;
                    notifyAll();
                    this.ˏ.ˊ(this.ॱ);
                    return true;
                }
            }
        }
        throw new AssertionError();
    }

    void ˏ(List<fwb> list) {
        if (ᐝ || !Thread.holdsLock(this)) {
            boolean ॱ;
            synchronized (this) {
                this.ˋॱ = true;
                this.ʼ.add(fvg.ˊ((List) list));
                ॱ = ॱ();
                notifyAll();
            }
            if (!ॱ) {
                this.ˏ.ˊ(this.ॱ);
                return;
            }
            return;
        }
        throw new AssertionError();
    }

    void ॱ(fxg o_fxg, int i) throws IOException {
        if (ᐝ || !Thread.holdsLock(this)) {
            this.ͺ.ˎ(o_fxg, (long) i);
            return;
        }
        throw new AssertionError();
    }

    void ʽ() {
        if (ᐝ || !Thread.holdsLock(this)) {
            boolean ॱ;
            synchronized (this) {
                this.ͺ.ˋ = true;
                ॱ = ॱ();
                notifyAll();
            }
            if (!ॱ) {
                this.ˏ.ˊ(this.ॱ);
                return;
            }
            return;
        }
        throw new AssertionError();
    }

    synchronized void ˋ(fwf o_fwf) {
        if (this.ʽ == null) {
            this.ʽ = o_fwf;
            notifyAll();
        }
    }

    void ॱॱ() throws IOException {
        if (ᐝ || !Thread.holdsLock(this)) {
            Object obj;
            boolean ॱ;
            synchronized (this) {
                obj = (!this.ͺ.ˋ && this.ͺ.ˏ && (this.ˎ.ˊ || this.ˎ.ˋ)) ? 1 : null;
                ॱ = ॱ();
            }
            if (obj != null) {
                ˎ(fwf.CANCEL);
                return;
            } else if (!ॱ) {
                this.ˏ.ˊ(this.ॱ);
                return;
            } else {
                return;
            }
        }
        throw new AssertionError();
    }

    void ˎ(long j) {
        this.ˊ += j;
        if (j > 0) {
            notifyAll();
        }
    }

    void ˏॱ() throws IOException {
        if (this.ˎ.ˋ) {
            throw new IOException("stream closed");
        } else if (this.ˎ.ˊ) {
            throw new IOException("stream finished");
        } else if (this.ʽ != null) {
            throw new StreamResetException(this.ʽ);
        }
    }

    void ͺ() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }
}
